package com.ymatou.iapi.usersafe.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.usersafe.parameter.RegistersafeCountBean;
import com.ymatou.iapi.usersafe.service.RegistersafeCountCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CounterCallService;
import com.ymttest.database.sqlwapper.UserSafeWapper;
import com.ymttest.utils.TestDataManager;
/**
*注册风控数据统计
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_RegistersafeCount{
	private static RegistersafeCountBean registersafecountBean;
	private static RegistersafeCountCall registersafecountCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("注册风控数据统计");
	}

	@Before
	public void caseUp() {
		registersafecountBean=new RegistersafeCountBean();
		registersafecountCall=new RegistersafeCountCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(registersafecountCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private RegistersafeCountBean initSafeCountBean() {
		RegistersafeCountBean rb=new RegistersafeCountBean();
		rb.setAppId("autotest");
		rb.setClientIp(TestDataManager.getRandomIp());
		rb.setDeviceId(TestDataManager.getRandomUUID().toString());
		rb.setLoginId("regsafetest_"+TestDataManager.getRandomNum(5));
		return rb;
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RegistersafeCount_001() {
		Logger.start(true,"注册风控数据统计成功");
		try {
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP");
			new UserSafeWapper().updateRiskStrategyStatus(1, "device");
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP_device");
			registersafecountBean=initSafeCountBean();
			registersafecountCall.setData(registersafecountBean);
			registersafecountCall.callService();
			
			Logger.verifyEquals("success", registersafecountCall.getJsonObject().get("LastErrorMessage").getAsString(), "验证LastErrorMessage");
			Logger.verifyEquals(200,  registersafecountCall.getJsonObject().get("ErrorCode").getAsInt(), "验证ErrorCode");
			Logger.verifyEquals(true,  registersafecountCall.getJsonObject().get("Success").getAsBoolean(), "验证Success");
			
			int ci = CounterCallService.getRegisterRiskSecond(registersafecountBean.getClientIp());
			Logger.verifyEquals(1, ci, "验证调用次数");
			int di = CounterCallService.getRegisterRiskSecond(registersafecountBean.getDeviceId());
			Logger.verifyEquals(1, di, "验证调用次数");
			int cad=CounterCallService.getRegisterRiskSecond(registersafecountBean.getClientIp()+"_"+registersafecountBean.getDeviceId());
			Logger.verifyEquals(1, cad, "验证调用次数");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_RegistersafeCount_002() {
		Logger.start(true,"不输入loginid,失败");
		try {
			registersafecountBean=initSafeCountBean();
			registersafecountBean.setLoginId(null);
			registersafecountCall.setData(registersafecountBean);
			registersafecountCall.callService();
			
			Logger.verifyIsContains("not empty", registersafecountCall.getJsonObject().get("LastErrorMessage").getAsString(), "验证LastErrorMessage");
			Logger.verifyEquals(100,  registersafecountCall.getJsonObject().get("ErrorCode").getAsInt(), "验证ErrorCode");
			Logger.verifyEquals(false,  registersafecountCall.getJsonObject().get("Success").getAsBoolean(), "验证Success");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_RegistersafeCount_003() {
		Logger.start(true,"调用多次count计数正确，成功");
		try {
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP");
			new UserSafeWapper().updateRiskStrategyStatus(1, "device");
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP_device");
			registersafecountBean=initSafeCountBean();
			registersafecountCall.setData(registersafecountBean);
			int size=10;
			for(int i=0;i<size;i++){
				registersafecountCall.callService();
			}
			
			Logger.verifyEquals("success", registersafecountCall.getJsonObject().get("LastErrorMessage").getAsString(), "验证LastErrorMessage");
			Logger.verifyEquals(200,  registersafecountCall.getJsonObject().get("ErrorCode").getAsInt(), "验证ErrorCode");
			Logger.verifyEquals(true,  registersafecountCall.getJsonObject().get("Success").getAsBoolean(), "验证Success");
			
			int ci = CounterCallService.getRegisterRiskSecond(registersafecountBean.getClientIp());
			Logger.verifyEquals(10, ci, "验证调用次数");
			int di = CounterCallService.getRegisterRiskSecond(registersafecountBean.getDeviceId());
			Logger.verifyEquals(10, di, "验证调用次数");
			int cad=CounterCallService.getRegisterRiskSecond(registersafecountBean.getClientIp()+"_"+registersafecountBean.getDeviceId());
			Logger.verifyEquals(10, cad, "验证调用次数");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}